package com.mid;

/**
 * Created by Lxk on 2019/7/6.
 */
public class Solution236 {

    private TreeNode findCommonLowestAncestor(TreeNode cur, TreeNode p, TreeNode q) {
        if (cur == null || cur == p || cur == q) {
            return cur;
        }

        TreeNode left = findCommonLowestAncestor(cur.left, p, q);
        TreeNode right = findCommonLowestAncestor(cur.right, p, q);
        if (left != null && right != null) {
            return cur;
        }
        return left != null ? left : right;
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        return findCommonLowestAncestor(root, p, q);
    }

}
